import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import * as gc from 'src/app/global/const';
import { JsonResult } from 'src/app/global/model';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class AuditService {

  private url = {
    'selectAllMill':gc.BACKEND_SERVER + '/mill/all',
    'audit':gc.BACKEND_SERVER + '/dev/audit',
    'insertUser':gc.BACKEND_SERVER + '/sysUser/insert', // post
    'saveUserMill':gc.BACKEND_SERVER + '/userMill/save'
  }
  constructor(
    private http:HttpClient
  ) { }

  audit(openId:string,realName:string,mill:string):Observable<JsonResult>{
    const url:string = this.url.audit + '/' + openId + '/' + realName + '/'+ mill;
    console.log('请求后端审核的URL：' + url);
    return this.http.get<JsonResult>(url);
  }

  insertUser(record:SysUser){
    const url:string = this.url.insertUser;
    return this.http.post<JsonResult>(url,record);
  }

  // 向表 wechat.user_mill 中写入数据，保存微信用户和 mill 的关系
  saveUserMill(record:UserMill){
    const url = this.url.saveUserMill;
    return this.http.post<JsonResult>(url,record);
  }

}

export class UserMill {
  iid:number;
  open_id:string;
  mill_sid:string;

  toString(){
    return `iid=${this.iid},open_id=${this.open_id},mill_sid=${this.mill_sid}`;
  }
}

export class SysUser {
  id:number;
  login_name:string;
  login_password:string;
  user_name:string;
  user_sex:string;
  openid:string;

  toString(){
    return 'id=' + this.id + ',login_name=' + this.login_name +',login_password='+this.login_password + ',user_name=' + this.user_name + ',user_sex='+ this.user_sex + ',openid=' + this.openid;
  }
}
